<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.staticfile.org/vue/3.0.5/vue.global.js"></script>
    <title>Document</title>
    <style>
        .red {
            color: red;
        }
        .blue{
            color: blue;
        }
    </style>
</head>

<body>

    <div id="app">

    </div>
    <template id="main">
        <div style="color: red;">
            {{main}} --- 孙子：{{twonum}}
            <hr>
            <firstvue></firstvue>

        </div>
    </template>
    <template id="first">
        <div style="color: blue;">
            {{first}}
            <br>
            <hr>
            <twovue></twovue>
        </div>
    </template>
    <template id="two">
        <div>
            twoNum: {{twoNum}}
            mainnum: {{mainnum}}
        </div>
    </template>
</body>

<script>
    //非父子组件 只能父-》子从上往下传递，不能反过来 子-》父传递
    let twoconfig = {
        template: '#two',

        //inject 直接在孙子组件中注入依赖
        inject:['mainnum'],
        methods:{
        },
        data() {
            return {
                two: 'two info',
                twoNum:0
            }
        },
        provide(){
            return{
                'twonum':this.twoNum
            }
        }
    };

    let firstconfig = {
        template: '#first',
        data() {
            return {
                first: 'first info',
                firstNum:0
            }
        },
        methods:{
        },
        components: {
            twovue: twoconfig
        }
    };

    let mainconfig = {
        template: '#main',
        inject:['twonum'],
        data() {
            return {
                main: 'main info',
                num :89
            }
        },
        //父组件中使用 provide写入
        provide(){
            return {
                'mainnum':this.num
            }
        },
        components: {
            firstvue: firstconfig
        }
    };

    Vue.createApp(mainconfig).mount("#app");
    // Vue.createApp(firstconfig).mount("#app");
    // Vue.createApp(twoconfig).mount("#app");

</script>

</html>